imports System.Text 
Imports System.Text.RegularExpressions
Imports System.IO
Public Class StringFunc

    Private Shared HTMLTAG_RULE As String = "<(.|\n)*?>"
    ''' <summary>
    ''' This function is to use Regular Expression to remove HTML Tag which we defined on the HTMLTAG_RULE
    ''' </summary>
    ''' <param name="OrigText"></param>
    ''' <returns>Text without HTML Tag</returns>
    ''' <remarks></remarks>
    Public Shared Function RemoveHTMLTag(ByVal OrigText As String) As String
        Return Regex.Replace(OrigText, HTMLTAG_RULE, String.Empty)
    End Function

    ''' <summary>
    ''' This function is used to return the first few text of the string
    ''' </summary>
    ''' <param name="OrigText"></param>
    ''' <param name="TextLimit"></param>
    ''' <returns>First Certain Text</returns>
    ''' <remarks></remarks>
    Public Shared Function Get_FirstCertainText(ByVal OrigText As String, ByVal TextLimit As Integer) As String
        Dim result As String = RemoveHTMLTag(OrigText)

        If result.Length > TextLimit Then
            result = result.Substring(0, TextLimit)
        End If
        Return result
    End Function

    ''' <summary>
    ''' THis function is used to read text from a file and return 
    ''' </summary>
    ''' <param name="FileToRead"> the Full filepath with filename to be read</param>
    ''' <returns>The file content</returns>
    ''' <remarks></remarks>
    Public Function ReadTextFromFile(ByVal FileToRead As String) As String
        Dim result As String = ""
        If File.Exists(FileToRead) Then
            Dim fReader As StreamReader
            fReader = New StreamReader(FileToRead)
            Try
                result = fReader.ReadToEnd()
            Finally
                fReader.Close()
            End Try
        Else
            Throw New Exception("File not found.")
        End If
        Return result
    End Function
    Public Shared Function GetTimeStamp() As String
       
        Return String.Format("{0:yyyyMMddhhmmss}", Now)
    End Function



    Public Function TextToReplace(ByVal OrigText As String, ByVal ReplaceText()() As String) As String
        Dim result As String = OrigText
        For i As Integer = 0 To ReplaceText.Length - 1
            If ReplaceText(i)(0) IsNot Nothing AndAlso ReplaceText(i)(1) IsNot Nothing Then
                result = result.Replace(ReplaceText(i)(0), ReplaceText(i)(1))
            End If
        Next
        Return result
    End Function

    Public Function ReadFileWithReplaceText(ByVal FileToRead As String, ByVal ReplaceText()() As String) As String
        Dim result As String = ""
        result = ReadTextFromFile(FileToRead)
        If result <> "" Then
            result = TextToReplace(result, ReplaceText)
        End If
        Return result
    End Function


    Public Shared Function AddSlash(ByVal DirectoryPath As String) As String
        Dim TempPath As String = DirectoryPath
        If (TempPath.Substring(TempPath.Length - 1) <> "\") Then
            TempPath = TempPath & "\"
        End If
        Return TempPath
    End Function
    ''' <summary>
    ''' instead of writing TextBox1.Text = iif(VarA IS NOTHING, "Default Value", VarA)
    ''' We can use TextBix1.Text = StringFunc(VarA) to assign value from variable which
    ''' you do not know if they are Null
    ''' </summary>
    ''' <param name="OrigText">Original Text</param>
    ''' <param name="DefaultValue">The Text you want to show when the Original Text is Null/Nothing</param>
    ''' <returns>String</returns>
    ''' <remarks></remarks>
    Public Shared Function Coalesce(ByVal OrigText As Object, Optional ByVal DefaultValue As String = "") As String
        Dim result As String = DefaultValue

        Try
            result = OrigText
        Finally
        End Try

        Return result
    End Function

    ''' <summary>
    ''' This function is used to retrieve the number part of the string 
    ''' </summary>
    ''' <param name="SourceString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Get_NumberFromString(ByVal SourceString) As String
        Dim RegExpress As New Regex("(\d*[.]\d*|\d+)")
        Return RegExpress.Match(SourceString).Groups(0).Value
    End Function
End Class
